package com.study.imageprocessing;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity implements OnItemSelectedListener {
	private static final int SELECT_PICTURE = 1;
	private String selectedImagePath;
	private Handler handler;
	private ProgressDialog progressDialog;
	private Button browseImage;
	private ImageView ivMain;
	private Spinner spOption;
	private Bitmap bm, bm1, bm2, bm3, bm4, bm5, bm6;
	// check whether original Image processed
	private boolean check_bm1 = false, check_bm2 = false, check_bm3 = false,
			check_bm4 = false, check_bm5 = false, check_bm6 = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		handler = new Handler();
		// Initialize ImageView
		ivMain = (ImageView) findViewById(R.id.ivMain);

		// Initialize Spinner
		spOption = (Spinner) findViewById(R.id.spOption);
		spOption.setOnItemSelectedListener(this);
		spOption.setId(0);

		// Set Adapter for Spinner
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.spOption, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spOption.setAdapter(adapter);

		bm = BitmapFactory.decodeResource(getResources(), R.raw.un);

		browseImage = (Button) findViewById(R.id.btnBrowse);
		browseImage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(
						Intent.createChooser(intent, "Select Picture"),
						SELECT_PICTURE);
			}
		});
	}

	// Customize Spinner Option
	@Override
	public void onItemSelected(AdapterView<?> adapter, View v, int position,
			long id) {
		chooseOption(position);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// Do nothing
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				spOption.setSelection(0);
				Uri selectedImageUri = data.getData();
				selectedImagePath = getPath(selectedImageUri);
				// Initialize Bitmap
				bm = BitmapFactory.decodeFile(selectedImagePath);
				ivMain.setImageBitmap(bm);
				check_bm1 = check_bm2 = check_bm3 = check_bm4 = check_bm5 = check_bm6 = false;
				spOption.setEnabled(true);
			}
		}
	}

	private void chooseOption(int position) {
		showProcessDialog(position);
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void showProcessDialog(final int position) {

		// display the progressbar on the screen
		progressDialog = ProgressDialog.show(this, "Please wait",
				"Processing Image");

		// start the time consuming task in a new thread
		Thread thread = new Thread() {

			public void run() {
				switch (position) {
				case 1:
					if (!check_bm1) {
						bm1 = ProcessFunction.invertImage(bm);
					}
					check_bm1 = true;
					break;
				case 2:
					if (!check_bm2) {
						bm2 = ProcessFunction.doGreyscale(bm);
					}
					check_bm2 = true;
					break;
				case 3:
					if (!check_bm3) {
						bm3 = ProcessFunction.applyFleaEffect(bm);
					}
					check_bm3 = true;
					break;
				case 4:
					if (!check_bm4) {
						bm4 = ProcessFunction.applyHueFilter(bm, 6);
					}
					check_bm4 = true;
					break;
				case 5:
					if (!check_bm5) {
						bm5 = ProcessFunction.applySnowEffect(bm);
					}
					check_bm5 = true;
					break;
				case 6:
					if (!check_bm6) {
						bm6 = ProcessFunction.binaryIamge(bm);
					}
					check_bm6 = true;
					break;
				}
				handler.post(new Runnable() {
					@Override
					public void run() {
						progressDialog.dismiss();
						switch (position) {
						case 0:
							// Display original Image
							ivMain.setImageBitmap(bm);
							break;
						case 1:
							// Display inverted Image
							ivMain.setImageBitmap(bm1);
							break;
						case 2:
							// Display grey scale Image
							ivMain.setImageBitmap(bm2);
							break;
						case 3:
							// Display flea effected Image
							ivMain.setImageBitmap(bm3);
							break;
						case 4:
							// Display Hue filter Image
							ivMain.setImageBitmap(bm4);
							break;
						case 5:
							// Display snow effected Image
							ivMain.setImageBitmap(bm5);
							break;
						case 6:
							// Display binary Image
							ivMain.setImageBitmap(bm6);
							break;
						}
					}
				});

			}

		};
		thread.start();
	}

}
